package com.mkl.system.controller;

import com.mkl.system.common.ActiveUser;
import com.mkl.system.common.ResultObj;
import com.mkl.system.domain.Notice;
import com.mkl.system.domain.User;
import com.mkl.system.service.NoticeService;
import com.mkl.system.vo.NoticeVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author king
 * @version 1.0
 * @date 2020/3/4 18:09
 */
@RequestMapping("api/notice")
@RestController
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 加载全部公告，条件搜索加载
     *
     * @param noticeVo 公告
     * @return 返回结果
     */
    @RequestMapping("loadAllNotice")
    public Object loadAllNotice(NoticeVo noticeVo) {
        return noticeService.queryAllNotice(noticeVo);
    }

    /**
     * 新增公告
     *
     * @param notice 公告
     * @return 返回新增结果 200success
     */
    @RequestMapping("addNotice")
    public ResultObj addNotice(Notice notice) {
        try {
            Subject subject = SecurityUtils.getSubject();
            ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
            User user = activeUser.getUser();
            notice.setOpername(user.getName() + "-" + user.getLoginname());
            notice.setCreatetime(new Date());
            noticeService.save(notice);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改公告
     *
     * @param notice 公告
     * @return 返回修改结果
     */
    @RequestMapping("updateNotice")
    public ResultObj updateNotice(Notice notice) {
        try {
            noticeService.updateById(notice);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id删除公告
     *
     * @param id 公告id
     * @return 返回结果
     */
    @RequestMapping("deleteNotice")
    public ResultObj DeleteNotice(Integer id) {
        try {
            noticeService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除公告
     *
     * @param ids 公告ids
     * @return 返回结果
     */
    @RequestMapping("batchDeleteNotice")
    public ResultObj batchDeleteNotice(Integer[] ids) {
        try {
            if (null != ids && ids.length > 0) {
                List<Integer> idsList = new ArrayList<>();
                Collections.addAll(idsList, ids);
                noticeService.removeByIds(idsList);
                return ResultObj.DELETE_SUCCESS;
            } else {
                return new ResultObj(-1, "删除数据不能为空！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}
